<!--
  - Copyright (c) 2019.  武汉中科图灵科技有限公司
  - Date :  2019/1/5
  - Author ：F12 Console=> atob("MzMxNDkxODAwQHFxLmNvbQ==")
  - Version ：1.6.2
  -->

<template>
  <div><div class="abstatic-height"></div>
  <div class="app-container calendar-list-container">
    <el-row>
      <div class="xf-tabletop-handles">
        <div class="xf-tabhd-left">
          <el-button class="filter-item" @click="handleCreate" type="primary" size="mini">添加测点</el-button>
        </div>
        <div class="xf-tabhd-right">
          测点
          <el-input v-model="listQuery.measuringPoint" placeholder="请输入测点" class="xf-query-input" size="mini"></el-input>
          <!--测点类型-->
          <!--<el-select v-model="listQuery.measuringPointType" placeholder="请选择" class="xf-query-select">-->
            <!--<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">-->
            <!--</el-option>-->
          <!--</el-select>-->
          <el-button @click="tableQuery" type="primary" size="mini">查询</el-button>
        </div>
      </div>
    </el-row>
    <el-table :key='tableKey' :data="list" v-loading.body="listLoading" style="width: 100%">

      <el-table-column label="序号" width="200">
        <template slot-scope="scope">
          <span>{{scope.row.id}}</span>
        </template>
      </el-table-column>
      <el-table-column label="测点">
        <template slot-scope="scope">
          <span>{{scope.row.measuringPoint}}</span>
        </template>
      </el-table-column>
      <el-table-column label="代号">
        <template slot-scope="scope">
          <span>{{scope.row.codeName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="数据单位">
        <template slot-scope="scope">
          <span>{{scope.row.dataUnit}}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="监测类型">-->
        <!--<template slot-scope="scope">-->
          <!--<span>{{scope.row.measuringPointType=='1'?'监测测点':'火警测点'}}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column label="操作" width="130px">
        <template slot-scope="scope">
          <span @click="handleUpdate(scope.row)" class="comm-table-btn"><icon-svg icon-class="bianji" style="fill:#50E3C2" /></span>
          <span @click="handleDelete(scope.row)" class="comm-table-btn"><icon-svg icon-class="shanchu" style="fill:#50E3C2" /></span>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[15,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </div>
    <slider-panel :Show.sync="dialogFormVisible" custom-class="out-side-frame">
      <div class="form-content-frame">
        <el-form :model="form" :rules="rules" ref="form" label-width="100px" label-position='top' @submit.native.prevent>
          <el-form-item label="测点" prop="measuringPoint">
            <el-input v-model.trim="form.measuringPoint" placeholder="请输入测点"></el-input>
          </el-form-item>
          <el-form-item label="代号" prop="codeName">
            <el-input v-model.trim="form.codeName" placeholder="请输入代号" :disabled="dialogStatus==='update'"></el-input>
          </el-form-item>
          <el-form-item label="数据单位" prop="dataUnit">
            <el-input v-model.trim="form.dataUnit" placeholder="请输入数据单位"></el-input>
          </el-form-item>
          <!--<el-form-item label="监测类型" prop="measuringPointType">-->
            <!--<el-radio v-model="form.measuringPointType" label="0" size="medium">火警测点</el-radio>-->
            <!--<el-radio v-model="form.measuringPointType" label="1" size="medium">监测测点</el-radio>-->
          <!--</el-form-item>-->
          <!--<div v-show="form.measuringPointType==0">-->
          <!--<el-form-item label="正常值" required >-->
            <!--<div class="measure-val-item">-->
              <!--<el-col :span="9">-->
                <!--<el-form-item prop="level">-->
                  <!--<el-select v-model="form.normallevel">-->
                    <!--<el-option label="≤" value="0"></el-option>-->
                    <!--<el-option label="＞" value="1"></el-option>-->
                  <!--</el-select>-->
                <!--</el-form-item>-->
              <!--</el-col>-->
              <!--<el-col :span="14">-->
                <!--<el-form-item prop="desc">-->
                  <!--<el-input type="text" v-model="form.normaldesc"></el-input>-->
                <!--</el-form-item>-->
              <!--</el-col>-->
            <!--</div>-->
          <!--</el-form-item>-->
          <!--<el-form-item label="一级预警值" prop="measuringPointType">-->
            <!--<div class="measure-val-item">-->
              <!--<el-col :span="9">-->
                <!--<el-form-item prop="level">-->
                  <!--<el-select v-model="form.firstlevel">-->
                    <!--<el-option label="≤" value="0"></el-option>-->
                    <!--<el-option label="＞" value="1"></el-option>-->
                  <!--</el-select>-->
                <!--</el-form-item>-->
              <!--</el-col>-->
              <!--<el-col :span="14">-->
                <!--<el-form-item prop="desc">-->
                  <!--<el-input type="text" v-model="form.firstdesc"></el-input>-->
                <!--</el-form-item>-->
              <!--</el-col>-->
            <!--</div>-->
          <!--</el-form-item>-->
          <!--<el-form-item label="二级预警值" prop="measuringPointType">-->
            <!--<div class="measure-val-item">-->
              <!--<el-col :span="9">-->
                <!--<el-form-item prop="level">-->
                  <!--<el-select v-model="form.secondlevel">-->
                    <!--<el-option label="≤" value="0"></el-option>-->
                    <!--<el-option label="＞" value="1"></el-option>-->
                  <!--</el-select>-->
                <!--</el-form-item>-->
              <!--</el-col>-->
              <!--<el-col :span="14">-->
                <!--<el-form-item prop="desc">-->
                  <!--<el-input type="text" v-model="form.seconddesc"></el-input>-->
                <!--</el-form-item>-->
              <!--</el-col>-->
            <!--</div>-->
          <!--</el-form-item>-->
          <!--</div>-->
            <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')" size="mini" native-type="submit">确 定</el-button>
            <el-button v-else type="primary" @click="update('form')" size="mini" native-type="submit">确 定</el-button>
            <el-button @click="cancel('form')" type="text" size="mini">取 消</el-button>
        </el-form>
      </div>
    </slider-panel>
    <!-- 删除操作弹窗 -->
    <el-dialog title="删除测点" :visible.sync="dialogDeleteVisible" :show-close='false' custom-class="xf-jc-dialog" width="360px">
      <div class="xf-jc-deletect" v-if="deleteJumpObj.length>0">
        <p>请将测点与设备类型解绑后再进行删除
        </p>
        <div>
          <el-button type="text" @click="openChildsites">
            前往解绑
            <i class="el-icon-d-arrow-right"></i>
          </el-button>
        </div>
      </div>
      <div class="xf-jc-deletect" v-else>
        <p>无子站使用此测点，确定删除？</p>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" size="mini" v-if="deleteJumpObj.length<1" @click="deleteD(currentSelectObj)">删除</el-button>
        <el-button type="text" size="mini" v-if="deleteJumpObj.length<1" @click="dialogDeleteVisible = false">取消</el-button>
        <el-button type="primary" size="mini" v-if="deleteJumpObj.length>0"  @click="dialogDeleteVisible = false">知道了</el-button>
      </div>
    </el-dialog>
    <childSiteDialog :Show.sync="childSitesShow" :siteIds.sync="childSiteids"></childSiteDialog>
  </div>
  </div>
</template>

<script>
import childSiteDialog from '@/components/ChildsiteList'

import {
  page,
  addObj,
  getObj,
  delObj,
  putObj,
  deleteQuery
} from "@/api/device/deviceMeasuringPoint/index";
import { mapGetters } from "vuex";
import sliderPanel from '@/components/SliderPanel'
export default {
  name: "deviceMeasuringPoint",
  components: {childSiteDialog,sliderPanel},
  data() {
    return {
      childSitesShow:false,
      childSiteids:'',
      options: [
        {
          value: "-1",
          label: "全部"
        },
        {
          value: "1",
          label: "监测测点"
        },
        {
          value: "0",
          label: "火警测点"
        }
      ],
      form: {
        measuringPoint: undefined,
        codeName: undefined,
        dataUnit: undefined,
        measuringPointType: "0"
      },
      rules: {
        measuringPoint: [
          {
            required: true,
            message: "请输入测点",
            trigger: "blur"
          },
          {
            min: 1,
            max: 8,
            message: "最多允许输入8个字符",
            trigger: "blur"
          }
        ],
        codeName: [
          {
            required: true,
            message: "请输入代号",
            trigger: "blur"
          },
          {
            min: 1,
            max: 100,
            message: "最多允许输入100个字符",
            trigger: "blur"
          }
        ],
        dataUnit: [
          {
            max: 50,
            message: "最多允许输入50个字符",
            trigger: "blur"
          }
        ],
        measuringPointType: [
          {
            required: true,
            message: "请选择测点类型",
            trigger: "blur"
          }
        ]
      },
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 15,
        measuringPoint: "",
        measuringPointType: "-1"
      },
      dialogFormVisible: false,
      dialogDeleteVisible:false,
      deleteJumpObj:'',
      currentSelectObj:{},
      dialogStatus: "",
      textMap: {
        update: "编辑测点",
        create: "添加测点"
      },
      tableKey: 0
    };
  },
  created() {
    this.getList();
  },
  methods: {
    openChildsites(){
      this.$router.push("/adminSys/deviceSeries")
    },
    tableQuery() {
      this.getList();
    },
    getList() {
      this.listLoading = true;
      page(this.listQuery).then(response => {
        this.list = response.data.rows;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    handlerest() {
      this.form.measuringPoint = undefined;
      this.form.codeName = undefined;
      this.form.dataUnit = undefined;
      this.form.measuringPointType = "0";
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleCreate() {
      //this.handlerest();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.form = {};
    },
    handleUpdate(row) {
      getObj(row.id).then(response => {
        this.form = response.data;
        this.dialogFormVisible = true;
        this.dialogStatus = "update";
      });
    },
    deleteD(row) {
      delObj(row.id).then(() => {
        this.dialogDeleteVisible = false;
        this.$notify({
          title: "成功",
          message: "删除成功",
          type: "success",
          duration: 2000
        });
        const index = this.list.indexOf(row);
        this.list.splice(index, 1);
        this.total=this.total-1;
      });
    },
    handleDelete(row) {
      let self = this;
      // 删除操作查询
      deleteQuery(row.id).then(response => {
        if (response.status === 200) {
          self.currentSelectObj = row;
          self.dialogDeleteVisible = true;
          self.deleteJumpObj = response.data;
          self.childSiteids = response.data.join(",")
        }
      });
    },
    create(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          addObj(this.form).then(() => {
            this.dialogFormVisible = false;
            this.$refs[formName].resetFields();
            this.handlerest();

            this.getList();
            this.$notify({
              title: "成功",
              message: "创建成功",
              type: "success",
              duration: 2000
            });
          });
        } else {
          return false;
        }
      });
    },
    cancel(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
      this.handlerest();
    },
    update(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false;
          putObj(this.form).then(() => {
            this.$notify({
              title: "成功",
              message: "修改成功",
              type: "success",
              duration: 2000
            });
            this.$refs[formName].resetFields();
            this.handlerest();
            this.getList();
          }).catch(()=>{
            this.$refs[formName].resetFields();
            this.handlerest();
          });
        } else {
          return false;
        }
      });
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss">
.xf-query-input,
.xf-query-select {
  width: 200px;
  margin: 0 10px;
}
.fulldialog > div {
  width: 500px;
  margin: 0 auto;
}
.fulldialog .el-dialog__title {
  font-size: 32px;
}
.fulldialog {
  .el-dialog__header {
    margin-top: 20px;
  }
  .custom-dialog-btn {
    position: absolute;
    font-size: 30px;
    left: 40px;
    top: 20px;
    color: #fff;
    &:hover {
      cursor: pointer;
    }
  }
  .dialog-footer {
    text-align: left;
  }
}
.measure-val-item{
  display:flex;
  justify-content:space-between;
}
</style>

